package com.sunnyebuy.merchant.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sunnyebuy.merchant.dao.MerchantDAO;
import com.sunnyebuy.merchant.po.Merchant;
import com.sunnyebuy.merchant.service.MerchantService;

/**
 * <p>
 * Title: MerchantServiceImpl.java
 * </p>
 * 
 * <p>
 * Description: com.sunnyebuy.merchant.service.impl
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2008-2011 All rights reserved.
 * </p>
 * 
 * <p>
 * Created: 2011-12-28 上午11:21:46
 * </p>
 * 
 * @author Wenzhong Gu
 * 
 * @version 1.0
 */
@Service(value = "merchantService")
public class MerchantServiceImpl implements MerchantService {

	private static final Logger log = LoggerFactory.getLogger(MerchantServiceImpl.class);

	@Resource
	private MerchantDAO merchantDAO;

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Merchant findMerchant(String email, String password) {
		try {
			return merchantDAO.findMerchant(email, password);
		} catch (Exception e) {
			log.error("=== method : findMerchant parameters : email={} , password={} ERROR ==={}", new String[] {
					email, password, e.getMessage() });
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Merchant loadMerchant(String merchantId) {
		try {
			return merchantDAO.loadMerchant(merchantId);
		} catch (Exception e) {
			log.error("=== method : loadMerchant parameters : merchantId={} ERROR ==={}",
					new String[] { merchantId, e.getMessage() });
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
	public Merchant updateMerchant(Merchant merchant) throws Exception {
		return merchantDAO.updateMerchant(merchant);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ecshopster.merchant.service.MerchantService#addMerchant(com.ecshopster .merchant.po.Merchant)
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
	public Merchant addMerchant(Merchant merchant) throws Exception {
		return merchantDAO.addMerchant(merchant);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ecshopster.merchant.service.MerchantService#findMerchantByEmail(java .lang.String)
	 */
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Merchant findMerchantByEmail(String email) {
		try {
			return merchantDAO.findMerchantByEmail(email);
		} catch (Exception e) {
			return null;
		}
	}
}